import random
import time


class Dish:
    def __init__(self, name, price, stock):
        self.name = name
        self.price = price
        self.stock = stock

    def info(self):
        print(f'菜品名称{self.name}, 价格{self.price}, 库存{self.stock}')


class Customer:
    def __init__(self, name):
        self.name = name
        self.orders = []

    def order_info(self):
        print(f'{self.name}的订单信息如下：')
        for order in self.orders:
            print(f"订单ID:{order['id']}, 客户:{order['customer']}, 菜品:{order['dish']}, 数量:{order['num']}")


class OrderManager:
    def __init__(self):
        self.dishes = {}
        self.orders = []

    def add_dish(self, dish):
        if dish.name in self.dishes:
            print(f'{dish.name}已经存在')
        else:
            self.dishes[dish.name] = dish
            print(f'{dish.name}添加成功')

    def rm_dish(self, dish):
        if dish.name in self.dishes:
            self.dishes.pop(dish)
            print(f'{dish.name}删除成功')
        else:
            print(f'{dish.name}不存在')

    @staticmethod
    def __generate_order_id():
        # 获取当前时间戳并格式化为指定格式
        timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
        # 可以添加随机数或其他标识符以确保唯一性
        unique_id = f"{timestamp}{str(time.time_ns() + random.randint(0, 999))[-5:]}"  # 取纳秒后5位
        return unique_id

    def show_dish(self):
        for dish in self.dishes.values():
            dish.info()

    def make_order(self, customer, dish_name, num):
        if dish_name in self.dishes and self.dishes[dish_name].stock >= num:
            # 生成订单
            order_id = self.__generate_order_id()
            while order_id in self.orders:
                order_id = self.__generate_order_id()
            order = {'id': order_id,
                     'customer': customer.name,
                     'dish': dish_name,
                     'num': num}

            # 订单收录系统
            self.orders.append(order)
            # 小票给顾客
            customer.orders.append(order)

            self.dishes[dish_name].stock -= num
            print('下单成功')
        else:
            print(f'{dish_name}没有库存了')

    def calculate_income(self):
        income = 0
        for order in self.orders:
            income += order['num'] * self.dishes[order['dish']].price
        return income

    def show_orders(self):
        for order in self.orders:
            print(f"订单ID:{order['id']}, 客户:{order['customer']}, 菜品:{order['dish']}, 数量:{order['num']}")


if __name__ == '__main__':
    ming = Customer('小明')
    mei = Customer('小美')
    dish_mapo = Dish('麻婆豆腐', 10, 100)
    dish_xiang = Dish('麻辣香锅', 20, 100)
    order_manager = OrderManager()
    order_manager.add_dish(dish_xiang)
    order_manager.add_dish(dish_mapo)
    dish_mapo.info()
    dish_xiang.info()

    order_manager.show_dish()
    print('---')
    order_manager.make_order(ming, '麻婆豆腐', 10)
    order_manager.make_order(mei, '麻辣香锅', 20)
    order_manager.make_order(mei, '麻婆豆腐', 120)
    order_manager.show_orders()
    mei.order_info()
    ming.order_info()
    dish_xiang.info()
    dish_mapo.info()
    print(f'当前收入：{order_manager.calculate_income()}')
